<template>
  <el-row>
    <el-row>
      <div style="width:100%;height:15px;padding:5px 5px 5px 10px;">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">
            <b style="font-size:16px;">书友网</b>
          </el-breadcrumb-item>
          <el-breadcrumb-item>
            <span style="font-size:16px;">注册页面</span>
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </el-row>
    <el-row>
      <el-card>
        <div style="width:50%;margin:auto">
          <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
          <el-divider>
            <i class="el-icon-mobile-phone"></i>
          </el-divider>
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="邮箱" prop="mailbox">
              <el-input
                type="mailbox"
                v-model="ruleForm.mailbox"
                autocomplete="off"
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>
            <el-form-item label="登陆用户名" prop="username">
              <el-input
                type="username"
                v-model="ruleForm.username"
                autocomplete="off"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item label="显示昵称" prop="nickname">
              <el-input
                type="nickname"
                v-model="ruleForm.nickname"
                autocomplete="off"
                placeholder="请输入昵称"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input
                type="password"
                v-model="ruleForm.pass"
                autocomplete="off"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <el-button
            style="letter-spacing:20px;text-indent: 20px;font:16px Medium;"
            type="primary"
            @click="submitForm('ruleForm')"
          >注册</el-button>
          <p style="text-align:center;line-height:60px;">
            ﹡点击 “注册” 按钮，即表示您同意并愿意遵守
            <a style="text-decoration:underline" href="#">用户协议</a>。
          </p>
        </div>
      </el-card>
    </el-row>
  </el-row>
</template>

<script>
export default {
  name: "user-register-page",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      checked: false,
      ruleForm: {
        username: "",
        pass: "",
        checkPass: "",
        mailbox: ""
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ],
        mailbox: [{ required: true, message: "请输入邮箱", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert("submit!");
          this.$alert("注册成功！5秒后自动跳转首页……", "提示", {
            confirmButtonText: "确定",
            type: 'success',
            callback: action => {
              this.$router.push({ path: "/" });
            }
          });
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style>
</style>